"""Walking session model."""
from datetime import datetime
from app import db
from app.models.base import BaseModel


class WalkingSession(BaseModel):
    """Walking session model representing a user's walk."""
    
    __tablename__ = 'walking_sessions'
    
    user_id = db.Column(db.String(36), db.ForeignKey('users.id'), nullable=False)
    route_id = db.Column(db.String(36), db.ForeignKey('routes.id'))
    start_time = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    end_time = db.Column(db.DateTime)
    visited_poi_ids = db.Column(db.JSON, default=list)
    current_latitude = db.Column(db.Float)
    current_longitude = db.Column(db.Float)
    status = db.Column(db.String(32), default='active')  # active/paused/completed
    total_distance = db.Column(db.Float, default=0.0)
    generated_music_ids = db.Column(db.JSON, default=list)
    route_history = db.Column(db.JSON, default=list)
    
    # Relationships
    user = db.relationship('User', back_populates='sessions')
    route = db.relationship('Route')
    
    def to_dict(self):
        """Convert session to dictionary."""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'route_id': self.route_id,
            'start_time': self.start_time.isoformat() if self.start_time else None,
            'end_time': self.end_time.isoformat() if self.end_time else None,
            'visited_poi_ids': self.visited_poi_ids,
            'current_location': {
                'latitude': self.current_latitude,
                'longitude': self.current_longitude
            } if self.current_latitude and self.current_longitude else None,
            'status': self.status,
            'total_distance': self.total_distance,
            'generated_music_ids': self.generated_music_ids,
            'route_history': self.route_history
        }
